﻿Public Class QuanLyChuyenKhoan


    Private Sub QuanLyChuyenKhoan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbNguoiChuyen.DataSource = ProcFunc.ExecuteFuncSQL("select * from [User]")
        cbNguoiChuyen.ValueMember = "MaUser"
        cbNguoiChuyen.DisplayMember = "TenUser"
        cbNguoiChuyen.SelectedIndex = -1

        cbNguoiNhan.DataSource = ProcFunc.ExecuteFuncSQL("select * from [User]")
        cbNguoiNhan.ValueMember = "MaUser"
        cbNguoiNhan.DisplayMember = "TenUser"
        cbNguoiNhan.SelectedIndex = -1

        cbTaiKhoanChuyen.DataSource = ProcFunc.ExecuteFuncSQL("select distinct MaNganHang, TenNganHang from NganHang")
        cbTaiKhoanChuyen.ValueMember = "MaNganHang"
        cbTaiKhoanChuyen.DisplayMember = "TenNganHang"
        cbTaiKhoanChuyen.SelectedIndex = -1

        cbTaiKhoanNhan.DataSource = ProcFunc.ExecuteFuncSQL("select distinct MaNganHang, TenNganHang from NganHang")
        cbTaiKhoanNhan.ValueMember = "MaNganHang"
        cbTaiKhoanNhan.DisplayMember = "TenNganHang"
        cbTaiKhoanNhan.SelectedIndex = -1
    End Sub

    Private Sub btnChuyenKhoan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChuyenKhoan.Click
        If cbNguoiChuyen.Text = "" Or cbNguoiNhan.Text = "" Or cbTaiKhoanChuyen.Text = "" Or cbTaiKhoanNhan.Text = "" Or txtSoTien.Text = "" Then
            MessageBox.Show("Vui lòng nhập đầy đủ thông tin!")
            Return
        Else

            If cbNguoiChuyen.Text = cbNguoiNhan.Text And cbTaiKhoanChuyen.Text = cbTaiKhoanNhan.Text Then
                MessageBox.Show("Không thể chuyển. Vui lòng xem lại thông tin đã nhập")
                Return
            End If

            'Tao ma chuyen khoan
            Dim str = "00"
            Dim day As String = str.Substring(0, 2 - dateNgay.Value.Day.ToString.Length) & dateNgay.Value.Day
            Dim month As String = str.Substring(0, 2 - dateNgay.Value.Month.ToString.Length) & dateNgay.Value.Month
            Dim year As String = dateNgay.Value.Year.ToString.Substring(dateNgay.Value.Year.ToString.Length - 2, 2)
            Dim mack = ProcFunc.ExecuteFuncSSQL("select max(MaChuyenKhoan) from ChuyenKhoan where MaChuyenKhoan like N'CK" & day & month & year & "%'").ToString()
            If mack = String.Empty Then
                mack = "CK" + day & month & year & "0001"
            Else
                mack = "CK" & (mack.Substring(2, mack.Length - 2) + 1)
            End If
            '//

            str = "insert into ChuyenKhoan values ('" & mack & "','" & cbNguoiChuyen.SelectedValue & "','" & cbNguoiNhan.SelectedValue &
                "','" & cbTaiKhoanChuyen.SelectedValue & "','" & cbTaiKhoanNhan.SelectedValue & "','" & String.Format("{0:yyyy-MM-dd}", dateNgay.Value) &
                "'," & txtSoTien.Value & ",N'" & txtMoTa.Text & "')"

            ProcFunc.ExecuteProcSQL(str)
            Try
                MessageBox.Show("Đã chuyển thành công!")
            Catch ex As Exception
                MessageBox.Show("Lỗi")
            End Try
            Return
        End If
    End Sub

End Class